package com.mw.job.config;

import com.mw.job.bean.AppProperties;
import com.mw.job.bean.DisruptorProperties;
import com.mw.job.bean.ZkProperties;
import com.mw.job.disruptor.DisruptorMain;
import com.mw.job.disruptor.DisruptorProducer;
import com.mw.job.zookeeper.CuratorClient;
import com.mw.job.zookeeper.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * Created by mengdexuan on 2017/7/3 10:54.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
public class BeanConfiguration {

	@Autowired
	DisruptorProperties disruptorProperties;

	@Autowired
	ZkProperties zkProperties;

	@Autowired
	DisruptorProducer producer;

	@Autowired
	AppProperties appProperties;

	@Bean(name = "scheduledTaskRegistrar")
	public ScheduledTaskRegistrar scheduledTaskRegistrar(){

		DisruptorMain.start(disruptorProperties.getBufferSize(),disruptorProperties.getConsumerThreadNum());
		producer.setRingBuffer(DisruptorMain.ringBuffer());//给生产者设置 ringBuffer，此句应放在注册任务之前

		ZkClient.connect(zkProperties.getConnectString(),zkProperties.getSessionTimeout(),appProperties);
		CuratorClient.connect(zkProperties.getConnectString());

		ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();

		//producer 注册后就进入了生产状态
		if (disruptorProperties.getCornTask()){//注册 corn 类型任务
			registrar.addCronTask(producer,disruptorProperties.getLoopTime());
		}else {//注册 fixedRate 类型任务
			long loopTime = Long.parseLong(disruptorProperties.getLoopTime());
			registrar.addFixedRateTask(producer,loopTime);
		}

		return registrar;
	}



}



















